home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / console / svgatext.3 / svgatext / SVGATextMode-1.3 / run_extprog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-05  |  2.0 KB  |  85 lines

  1. /*  SVGATextMode -- An SVGA textmode manipulation/enhancement tool
  2.  *
  3.  *  Copyright (C) 1995,1996  Koen Gadeyne
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License
  16.  *  along with this program; if not, write to the Free Software
  17.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20.  
  21. /***
  22.  *** run_extprog.c: function to call on external program.
  23.  ***/
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "misc.h"
  29. #include "run_extprog.h"
  30. #include "messages.h"
  31.  
  32. int Run_extern_Prog(char *commandline)
  33. {
  34.   int result=0;
  35.  
  36.   PDEBUG(("Executing external command `%s'\n", commandline));
  37.   result=system(commandline);
  38.   if (result !=0)
  39.   {
  40.     perror(commandline);
  41.     PERROR(("'%s' failed with error code %d\n", commandline, result));
  42.   }
  43.   return(result);
  44. }
  45.  
  46. #define FB_FRAGSIZE 1024
  47.  
  48. static char *extout;
  49.  
  50. int Run_extern_Prog_pipe(char *commandline)
  51. /* can't use PDEBUG etc. here, because S3 HS text mode would cause garbled output */
  52. {
  53.   int num=0;
  54.   char *extprog;
  55.   FILE *fp_extout;
  56.   
  57.   extprog = strcat(commandline, " 2>&1");
  58.   fp_extout=popen(extprog,"r");
  59.   if (fp_extout==NULL)
  60.   {
  61.     perror(commandline);
  62.     return(-1);
  63.   }
  64.  
  65.   /* save pipe output to buffer */
  66.   extout = (char *)malloc(FB_FRAGSIZE);
  67.   while ( (extout[num++]=fgetc(fp_extout))!=EOF ) 
  68.   {
  69.     if ( (num % FB_FRAGSIZE) == FB_FRAGSIZE-1)
  70.     {
  71.       extout=realloc((void *)extout, num+1+FB_FRAGSIZE);
  72.     }
  73.   }
  74.   extout[--num]='\0';
  75.   pclose(fp_extout);
  76.   return(0);
  77. }
  78.  
  79. inline void show_extout()
  80. {
  81.   printf("%s", extout);  
  82. }
  83.  
  84.  
  85.